package com.cooper.mediadb;

import java.util.ArrayList;
import java.util.List;

import com.cooper.scrapemedialib.R;
import com.cooper.scraper.show.AdapterDataChangedListener;
import com.cooper.scraper.show.FanartAdapter;
import com.cooper.scraper.show.FanartViewStrategy;
import com.cooper.scraper.show.FanartsShow;
import com.cooper.scraper.show.FlowGalleryViewFactory;
import com.cooper.scraper.show.ResourceCommandAction;
import com.cooper.scraper.show.ResourceShowListener;
import com.cooper.scraper.show.TileGalleryViewFactory;
import com.cooper.slidingmenu.utils.SampleDialogBaseFragment;
import com.cooper.slidingmenu.utils.SingleFragmentContain;

import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public abstract class MediaBrowseBaseFragment<T extends Media> extends
		SampleDialogBaseFragment implements AdapterDataChangedListener,
		ResourceShowListener<T> {

	public static final int AD_VIEW_HEIGHT = 76;

	protected LinearLayout actionBarFrame;

	private RelativeLayout mainLayout;

	private View noResultMessage;
	private View rightWait;

	private boolean tileGallery;

	protected FanartsShow<T> fanartsViewShow = null;

	protected T currentMedia;

	public T getCurrentMedia() {
		return currentMedia;
	}

	protected void switchMediaViewShow() {
		if (fanartsViewShow != null) {
			if ((tileGallery && fanartsViewShow instanceof TileGalleryViewFactory)
					|| (!tileGallery && fanartsViewShow instanceof FlowGalleryViewFactory))
				return;
			fanartsViewShow.removeFrom(mainLayout);
			fanartsViewShow.destroy();
		}
		if (tileGallery) {
			fanartsViewShow = new TileGalleryViewFactory<T>(getActivity()
					.getApplicationContext(), getViewStrategy(), getActivity()
					.getWindowManager().getDefaultDisplay().getWidth(),
					getGalleryViewHeight(), Media.HEIGHT_WIDHT_SCALE,
					R.drawable.missing);

		} else
			fanartsViewShow = new FlowGalleryViewFactory<T>(getActivity()
					.getApplicationContext(), getViewStrategy(), getActivity()
					.getWindowManager().getDefaultDisplay().getWidth(),
					getGalleryViewHeight(), Media.HEIGHT_WIDHT_SCALE,
					R.drawable.missing);

		FanartAdapter<T> adapter = getFanartAdapter();
		adapter.setDataChangedListener(this);
		// TODO test this
		fanartsViewShow.registerContextMenu(this);

		fanartsViewShow.setAdapter(adapter);

		fanartsViewShow.setCommands(commands);
		fanartsViewShow.showTo(mainLayout);

		// fanartsViewShow.setAdapter(fanartAdapter);
		// fanartsViewShow.registerContextMenu(this);
		// fanartsViewShow.setCommands(commands);
		// fanartsViewShow.showTo(mainLayout);
		fanartsViewShow.setMediasShowListener(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initCommandAction();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View result = inflater.inflate(getContentView(), container, false);
		findView(result);
		return result;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		tileGallery = PreferenceManager.getDefaultSharedPreferences(
				getActivity()).getBoolean("perfernce_tile_fanart", false);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if (fanartsViewShow != null) {
			fanartsViewShow.destroy();
		}
	}

	@Override
	public void dataWaiting(int direction) {
		if (noResultMessage != null)
			noResultMessage.setVisibility(View.GONE);
		if (rightWait !=  null) {
			rightWait.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void dataComplete(int direction) {
		if (rightWait !=  null) {
			rightWait.setVisibility(View.GONE);
		}
		// WaitNextDataListener.DIRECTION_DOWN
	}

	@Override
	public void noResultData() {
		if (noResultMessage != null)
			noResultMessage.setVisibility(View.VISIBLE);
	}

	protected void showMovieDetails(T media) {
		currentMedia = media;
		showMediaDetailsDialog();
	}

	protected List<ResourceCommandAction<T>> commands;

	protected void initCommandAction() {
		commands = new ArrayList<ResourceCommandAction<T>>();
		ResourceCommandAction<T> actionCommand = new ResourceCommandAction<T>() {
			@Override
			public void execute(T media) {
				playMedia(media);
			}
		};
		actionCommand.setGroup(0);
		actionCommand.setIconResId(R.drawable.ic_play);
		actionCommand.setTitle(getResources().getString(
				R.string.media_menu_play));
		commands.add(actionCommand);

		actionCommand = new ResourceCommandAction<T>() {
			@Override
			public void execute(T media) {
				showMovieDetails(media);
			}
		};
		actionCommand.setGroup(0);
		actionCommand.setIconResId(R.drawable.ic_details);
		actionCommand.setTitle(getResources().getString(
				R.string.media_menu_info));
		commands.add(actionCommand);

	}

	protected void findView(View view) {
		mainLayout = (RelativeLayout) view.findViewById(R.id.main_layout);

		noResultMessage = view.findViewById(R.id.no_result_movie);
		rightWait = view.findViewById(R.id.right_waiting);

		actionBarFrame = (LinearLayout) view.findViewById(R.id.action_layout);

		((ImageButton) view.findViewById(R.id.nav_button))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						((SingleFragmentContain) getActivity())
								.showNavigation();
					}
				});

		/*
		 * ImageButton showOfferBtn = ((ImageButton) view
		 * .findViewById(R.id.show_offer_wall_button));
		 * 
		 * if (showOfferBtn != null) { showOfferBtn.setOnClickListener(new
		 * OnClickListener() {
		 * 
		 * @Override public void onClick(View v) {
		 * YoumiOffersManager.showOffers(getActivity() .getApplicationContext(),
		 * YoumiOffersManager.TYPE_REWARD_OFFERS); } }); if
		 * (!MainActivity.showOffer){ showOfferBtn.setVisibility(View.GONE); } }
		 */
	}

	protected void showMediaDetailsDialog() {
		MediaInfoDialog mediaInfoDialog = new MediaInfoDialog();
		FragmentManager fm = getActivity().getSupportFragmentManager();
		mediaInfoDialog.show(fm, "MEDIA_INFO_DIALOG");
	}

	protected void showContinuePlayMediaDialog() {
		new MediaMainActivity.ContinuePlayMediaDialog().show(getActivity()
				.getSupportFragmentManager(), "CONTINUE_PLAY_DIALOG");
	}

	protected void playMedia(List<Uri> uris, String title, Uri uri, int position) {
		((MediaMainActivity) getActivity()).playMedia(uris, title, uri,
				position);
	}

	protected void playMedia(List<Uri> uris, String title) {
		((MediaMainActivity) getActivity()).playMedia(uris, title);
	}

	protected abstract void onMediaPlayResult(Uri uri, int position);

	protected abstract FanartViewStrategy<T> getViewStrategy();

	protected abstract int getGalleryViewHeight();

	protected abstract void onSelectPlayMedia(boolean continued);

	protected abstract void playMedia(T media);

	protected abstract int getContentView();

	protected abstract FanartAdapter<T> getFanartAdapter();
}
